package Concurrency2.Chapter06_AQS;

import java.util.concurrent.TimeUnit;

/**
 * Instruction：
 * 模板方法类: 1, 模板方法; 2, 钩子函数(子类实现)
 *
 * 使用
 * 父类引用指向子类引用
 * --子类方法
 * Author：@author MaLi
 */
public class T04_TemplateMethod {
    public static void main(String[] args) {
        ParentClass someClass = new SomeClass();//抽象类指向具体的实现类
        someClass.templateMethod();
    }
    //测试模板方法模式
    private static class ParentClass
    {
        public final void templateMethod() {
            long start = System.currentTimeMillis();
            hookMethod();
            long end = System.currentTimeMillis();
            System.out.println("Time wasted: " + (end - start));
        }

        protected void hookMethod() {
            throw new RuntimeException("Must implements this method...");
        }
    }
    //任意的实现类
    private static class SomeClass extends ParentClass {
        public void doSome() {
            try {
                System.out.println("DoSome is running...");
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        @Override
        protected void hookMethod() {
            doSome();//钩子函数中任意自己的实现.
        }
    }
}
